package com.osti.mytest;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class ActivityRoundRobin extends FragmentActivity
{
	private int			m_NrPlayers;
	private ImageView   m_btnMatches;
	private ImageView	m_btnTable;
	private String		m_PlayerNames;
	private String		m_homeaway;
	
	
	
	

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    	
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_round_robin);
    	
    	m_btnMatches = (ImageView)findViewById(R.id.iv_matches_rr);
    	m_btnTable= (ImageView)findViewById(R.id.iv_table_rr);
    	
    	//ActionBar Spiele ist am Anfang Aktiv
    	m_btnMatches.setImageResource(R.drawable.btn_ab_spiele_pressed);
    	
    	// Bundle for the information from "ActivityCntPlayers"
    	Bundle	h_BundleCntPlayers;
    	
    	
    	
    	
    	h_BundleCntPlayers = getIntent().getExtras();
        if(h_BundleCntPlayers != null)
        {
        	// get the following information from the Bundle:
        	// a) the number of players
        	m_NrPlayers		= h_BundleCntPlayers.getInt("key_Nr");
        	// a) the names of players
        	m_PlayerNames	= h_BundleCntPlayers.getString("key_Names");
        	// a) get homeaway status
        	m_homeaway		= h_BundleCntPlayers.getString("HomeAway");
        	
        }
        
        //Beim ersten Aufruf der Activity wird die Tabelle erstellt(Spielpaarungen)
        generate_matches();
        
        
        //Button von ActionBar-->Spiele
        m_btnMatches.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//Neues Bild setzen, um Effekt des Buttons zu realisieren
				m_btnMatches.setImageResource(R.drawable.btn_ab_spiele_pressed);
				m_btnTable.setImageResource(R.drawable.btn_ab_tabelle_disabled);
				generate_matches();
				
			}
        	
        });
        
        //Button von ActionBar--> Tabelle
        m_btnTable.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Neues Bild setzen, um Effekt des Buttons zu realisieren
				m_btnTable.setImageResource(R.drawable.btn_ab_tabelle_pressed);
				m_btnMatches.setImageResource(R.drawable.btn_ab_spiele_disabled);
				
				generate_table();			
			}
		});
        
    }
    //Methode um das Fragement FragRrMatches zu starten
    private void generate_matches()
    {
    	
    	Fragment			h_Frag;
    	Bundle				h_Args;
    	FragmentTransaction	h_FragTrans;
    	

			h_Frag = new FragRrMatches();
			h_Args = new Bundle();

			// add information
		  	h_Args.putInt(Constants.FRAG_KEY_PLAYERS, m_NrPlayers);
		  	h_Args.putString(Constants.FRAG_KEY_NAMES, m_PlayerNames);
		  	h_Args.putString("HomeAway", m_homeaway);
		  	h_Frag.setArguments(h_Args);
		  	
		  	
		  	// start fragment
		  	h_FragTrans = getSupportFragmentManager().beginTransaction();
		  	h_FragTrans.replace(R.id.grpMatches_container, h_Frag, Constants.FRAG_TAG_ROUNDROBIN_MATCHES);
		  	h_FragTrans.commit();

    	
    }
        	
    //Methode um das Fragement FragRrTable zu starten
    private void generate_table()
    {
    	Fragment			h_Frag;
    	Bundle				h_Args;
    	FragmentTransaction	h_FragTrans;
    	
		h_Frag = getSupportFragmentManager().findFragmentByTag(Constants.FRAG_TAG_ROUNDROBIN_TABLE);
		if(h_Frag == null)
		{
			h_Frag = new FragRrTable();
			h_Args = new Bundle();

			// add information
		  	h_Args.putInt(Constants.FRAG_KEY_PLAYERS, m_NrPlayers);
		  	h_Args.putString(Constants.FRAG_KEY_NAMES, m_PlayerNames);
		  	h_Args.putString("HomeAway", m_homeaway);
		  	h_Frag.setArguments(h_Args);
		  	
		  	// start fragment
		  	h_FragTrans = getSupportFragmentManager().beginTransaction();
		  	h_FragTrans.replace(R.id.grpMatches_container, h_Frag, Constants.FRAG_TAG_ROUNDROBIN_TABLE);
		  	h_FragTrans.commit();
		 }
    }
    
  
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
